<?php
/**
 * 后台返利提款表操作模型
 */
class MAffiliateWithdraw extends MY_Model {
	protected $table = 'affiliate_withdraw';
	/**
	 * 日、周、月
	 * @var array
	 */
	static $cycleLst = array(
	    '1'=>'%y%m%d',
	    '2'=>'%Y%u',
	    '3'=>'%Y%m'
	);
	function __construct() {
		parent::__construct();
	}
	
	/**
	 * 提款列表查询
	 * @param array $limit 分页参数
	 * @param bool $sort 排序标志
	 */
	public function getList($limit, $sort=false){
		$this->db->select($this->table.'.*,user.account,user_bank.card_no');
		$this->db->distinct();
		$this->db->from($this->table);
		$this->db->join('user', $this->table.'.user_id=user.id','LEFT');
		$this->db->join('user_bank', $this->table.'.bank_id=user_bank.id','LEFT');
		if($limit){
			$this->db->limit($limit['limit'],$limit['offset']);
		}
		$this->formfilter->doFilter();
		if($sort){
			$this->db->order_by($this->table.'.id', 'asc');
		}else{
			$this->db->order_by($this->table.'.id', 'desc');
		}
		$query = $this->db->get();
		return $query->result();
	}
	
	/**
	 * 关联订单列表计数
	 * @param array $indexWhere 关联索引表的条件语句
	 */
	public function getCount(){
		$this->db->select('count(distinct '.$this->db->dbprefix($this->table).'.id) as num');
		$this->db->from($this->table);
		$this->db->join('user', $this->table.'.user_id=user.id','LEFT');
		$this->db->join('user_bank', $this->table.'.bank_id=user_bank.id','LEFT');
		$this->formfilter->doFilter();
		$tmp = $this->db->get()->result();
		return (int)$tmp[0]->num;
	}
	
	/**
	 * 得到最后一条记录
	 */
	public function getLast(){
	    $this->db->select('*');
	    $this->db->from($this->table);
	    $this->db->limit(1);
	    $this->db->order_by('id','desc');
	    $result = $this->db->get()->result();
	    return $result[0];
	}
	
	
	/** 更新订单信息 */
	public function save($order, $log = NULL, $update_index = TRUE){
		$this->load->model('MOrderIndex');
		$this->db->trans_start();
		$success = parent::save($order);
		if($success && $update_index){
			foreach ($this->index_cols as $col){
				if(property_exists($order,$col) && strlen($order->{$col}) > 0){
					$this->MOrderIndex->save((object)array(
							'order_id'=>$order->id,
							'table_name'=>$this->table,
							'col_name'=>$col,
							'col_value'=>$order->{$col}
					));
				}
			}
		}
		if($success && $log){
			$this->load->model('MOrderLog');
			$log->order_id = $order->id;
			$log->payment_state = $order->payment_state;
			$log->delivery_state = $order->delivery_state;
			$log->state = $order->state;
			$log->risk = $order->risk;
			$log->create_time = $this->update_time = time();
			$this->MOrderLog->add($log);
		}
		return $this->db->trans_complete();
	}
	
	/**
	 * 返利红利报表
	 */
	public function getAffiliateWithdrawChart(){
	    $result = array(
	        'total'=>$this->_withdrawStatusOrder(2),
	        'not_paid'=>$this->_withdrawStatusOrder(0),
	        'paid'=>$this->_withdrawStatusOrder(1),
	        'canceled'=>$this->_withdrawStatusOrder(-1)
	    );
	    return $result;
	}
	
	private function _withdrawStatusOrder($withdrawStatus){
	    $format = filterValue('cycle')?self::$cycleLst[filterValue('cycle')]:self::$cycleLst[1];
	    if(filterValue('show_type') == 2){ //显示订单金额
	        $this->db->select(array('FROM_UNIXTIME(create_time,"'.$format.'") cycle','SUM(amount) count'));
	    }else{ //显示订单数量
	        $this->db->select(array('FROM_UNIXTIME(create_time,"'.$format.'") cycle','count(id) count'));
	    }
	    
	    $this->db->from($this->table);
	    $this->formfilter->doFilter();
	    if($withdrawStatus!=2){ //$withdrawStatus=2是全部订单
	        $this->db->where('status',$withdrawStatus);
	    }
	    $this->db->group_by('cycle');
	    
	    $rtn = $this->db->get()->result();
	    if($withdrawStatus==2){ //如果为全部订单直接返回
	        return $rtn;
	    }else{ //其他订单，将cycle做为键再返回
	        $karr = array();
	        foreach ($rtn as $k=>$v){
	            $karr[$v->cycle] = $v;
	        }
	        return $karr;
	    }
	    
	}

}
